﻿using System;
using System.Collections.Generic;
using System.Data;
using RpcHelper;
using RpcModel;

using SqlExecHelper;
using SqlExecHelper.SetColumn;
using Wedonek.RpcStore.Service.Helper;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
    internal class SysConfigDAL : SqlBasicClass
    {
        public SysConfigDAL() : base("SysConfig")
        {

        }
        public SysConfigDatum[] Query(QuerySysParam query, IBasicPage paging, out long count)
        {

            return base.Query<SysConfigDatum>("Id desc", paging.Index, paging.Size, out count, query.ToWhere());
        }

        public bool CheckIsExists(SysConfigAddParam config)
        {
            List<ISqlWhere> wheres = new List<ISqlWhere>(3);
            if (config.ServerId != 0)
            {
                wheres.Add(new SqlWhere("ServerId", SqlDbType.BigInt) { Value = config.ServerId });
            }
            else if (config.RpcMerId != 0)
            {
                wheres.Add(new SqlWhere("RpcMerId", SqlDbType.BigInt) { Value = config.RpcMerId });
            }
            if (!config.SystemType.IsNull())
            {
                wheres.Add(new SqlWhere("SystemType", SqlDbType.VarChar, 50) { Value = config.SystemType });
            }
            wheres.Add(new SqlWhere("Name", SqlDbType.VarChar, 50) { Value = config.Name });
            return base.CheckIsExists(wheres.ToArray());
        }

        public SysConfigDatum Get(long Id)
        {
            return base.GetRow<long, SysConfigDatum>("Id", Id);
        }
        public long Add(SysConfigAddParam add)
        {
            base.Insert(add, out long id);
            return id;
        }

        internal SysConfigDatum FindConfig(string systemType, string key)
        {
            return base.GetRow<SysConfigDatum>(new ISqlWhere[] {
                               new SqlWhere("SystemType", SqlDbType.VarChar,50) { Value =systemType },
                               new SqlWhere("Name", SqlDbType.VarChar, 50) { Value =key}
                        });
        }

        public void Set(long id, SysConfigSetParam config)
        {
            if (!base.Update(config, "Id", id))
            {
                throw new ErrorException("rpc.sys.config.set.error");
            }
        }
        public void SetIsEnable(long id, bool isEnable)
        {
            if (!base.Update(new ISqlSetColumn[] {
                                new SqlSetColumn("IsEnable", SqlDbType.Bit){Value=isEnable},
                                new SqlSetColumn("ToUpdateTime", SqlDbType.DateTime){ Value=DateTime.Now}
                        }, "Id", id))
            {
                throw new ErrorException("rpc.sys.config.set.error");
            }
        }
        public void Drop(long id)
        {
            if (!base.Drop("Id", id))
            {
                throw new ErrorException("rpc.sys.config.drop.error");
            }
        }

        public void Clear(long serverId)
        {
            base.Drop(new ISqlWhere[] {
                                new SqlWhere("ServerId", SqlDbType.BigInt){Value=serverId}
                        });
        }
    }
}
